﻿CREATE PROCEDURE [dbo].[Perfiles_DevolverTodos]

AS

SELECT p.IdPerfil,
	p.Nombre,
	p.Observacion
	FROM Perfiles p
	ORDER BY p.Nombre
	
SELECT ISNULL(pd.IdPerfilDetalle, 0) AS IdPerfilDetalle,
	p1.IdPerfil,
	pe.IdPermiso,
	CAST((CASE WHEN ISNULL(pd.IdPerfilDetalle, 0) = 0
			THEN 0
			ELSE 1 END) AS BIT) AS TienePermiso
	FROM Perfiles p1
		CROSS JOIN Permisos pe
		INNER JOIN Perfiles p2 
			ON p1.IdPerfil = p2.IdPerfil
		LEFT JOIN PerfilesDetalles pd 
			ON pe.IdPermiso = pd.IdPermiso 
			AND p1.IdPerfil = pd.IdPerfil